<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="180px">
    <el-form-item label="合同名称" prop="contractName">
      <el-input v-model="dataForm.contractName" placeholder="合同名称"></el-input>
    </el-form-item>
    <el-form-item label="合同编号" prop="contractNum">
      <el-input v-model="dataForm.contractNum" placeholder="合同编号"></el-input>
    </el-form-item>
    <el-form-item label="甲方" prop="firstParty">
      <el-input v-model="dataForm.firstParty" placeholder="甲方"></el-input>
    </el-form-item>
    <el-form-item label="乙方" prop="secondParty">
      <el-input v-model="dataForm.secondParty" placeholder="乙方"></el-input>
    </el-form-item>
    <el-form-item label="选择合同起止日期" prop="startTime">
      <!-- <el-input v-model="dataForm.startTime" placeholder="选择合同起止日期"></el-input> -->
      <!-- <div class="block"> -->
      <!-- <span class="demonstration">带快捷选项</span> -->
        <el-date-picker
          v-model="contractDateRange"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions2">
        </el-date-picker>
      <!-- </div> -->
    </el-form-item>

    

    <!-- <el-form-item label="合同结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="合同结束时间"></el-input>
    </el-form-item> -->
    <el-form-item label="合同金额（元）" prop="contractPrice">
      <el-input v-model="dataForm.contractPrice" placeholder="合同金额（元）"></el-input>
    </el-form-item>
    <!-- <el-form-item label="合同状态   0：在库   1： 借出" prop="contractType">
      <el-input v-model="dataForm.contractType" placeholder="合同状态   0：在库   1： 借出"></el-input>
    </el-form-item> -->
    <el-form-item label="合同简要描述" prop="contractDesc">
      <el-input v-model="dataForm.contractDesc" placeholder="合同简要描述"></el-input>
    </el-form-item>
    <el-form-item label="付款进度" prop="paymentProgress">
      <el-input v-model="dataForm.paymentProgress" placeholder="付款进度"></el-input>
    </el-form-item>
    <el-form-item label="合同存放位置" prop="storageLocation">
      <el-input v-model="dataForm.storageLocation" placeholder="合同存放位置"></el-input>
    </el-form-item>
    <!-- <el-form-item label="合同签订时间" prop="signingTime">
      <el-input v-model="dataForm.signingTime" placeholder="合同签订时间"></el-input>
    </el-form-item> -->

    <el-form-item label="合同签订时间" prop="signingTime">
      <el-date-picker
        v-model="dataForm.signingTime"
        type="datetime"
        placeholder="选择合同签订时间"
        align="right"
        :picker-options="signingTimePickerOptions">
      </el-date-picker>
    </el-form-item>

    <el-form-item label="录入人" prop="inputPerson">
      <el-input v-model="dataForm.inputPerson" placeholder="录入人"></el-input>
    </el-form-item>
    <!-- <el-form-item label="合同录入时间" prop="inputTime">
      <el-input v-model="dataForm.inputTime" placeholder="合同录入时间"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        signingTimePickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        pickerOptions2: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        visible: false,
        contractDateRange: '',
        dataForm: {
          id: 0,
          contractName: '',
          contractNum: '',
          firstParty: '',
          secondParty: '',
          startTime: '',
          endTime: '',
          contractPrice: '',
          contractType: '',
          contractDesc: '',
          paymentProgress: '',
          storageLocation: '',
          signingTime: '',
          inputPerson: '',
          inputTime: ''
        },
        dataRule: {
          // contractName: [
          //   { required: true, message: '合同名称不能为空', trigger: 'blur' }
          // ],
          // contractNum: [
          //   { required: true, message: '合同编号不能为空', trigger: 'blur' }
          // ],
          // firstParty: [
          //   { required: true, message: '甲方不能为空', trigger: 'blur' }
          // ],
          // secondParty: [
          //   { required: true, message: '乙方不能为空', trigger: 'blur' }
          // ],
          // startTime: [
          //   { required: true, message: '合同开始时间不能为空', trigger: 'blur' }
          // ],
          // endTime: [
          //   { required: true, message: '合同结束时间不能为空', trigger: 'blur' }
          // ],
          // contractPrice: [
          //   { required: true, message: '合同金额（元）不能为空', trigger: 'blur' }
          // ],
          // contractType: [
          //   { required: true, message: '合同状态   0：在库   1： 借出不能为空', trigger: 'blur' }
          // ],
          // contractDesc: [
          //   { required: true, message: '合同简要描述不能为空', trigger: 'blur' }
          // ],
          // paymentProgress: [
          //   { required: true, message: '付款进度不能为空', trigger: 'blur' }
          // ],
          // storageLocation: [
          //   { required: true, message: '合同存放位置不能为空', trigger: 'blur' }
          // ],
          // signingTime: [
          //   { required: true, message: '合同签订时间不能为空', trigger: 'blur' }
          // ],
          // inputPerson: [
          //   { required: true, message: '录入人不能为空', trigger: 'blur' }
          // ],
          // inputTime: [
          //   { required: true, message: '合同录入时间不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            console.log("修改合同的 id 是："+this.dataForm.id)

            // this.$http({
            //   url: this.$http.adornUrl(`/contract/sc hoolcontract/info/${this.dataForm.id}`),
            //   method: 'get',
            //   params: this.$http.adornParams()
            // })
            this.$axios.get(`/contract/schoolcontract/info/${this.dataForm.id}`)
            .then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.contractName = data.schoolContract.contractName
                this.dataForm.contractNum = data.schoolContract.contractNum
                this.dataForm.firstParty = data.schoolContract.firstParty
                this.dataForm.secondParty = data.schoolContract.secondParty
                this.dataForm.startTime = data.schoolContract.startTime
                this.dataForm.endTime = data.schoolContract.endTime
                this.dataForm.contractPrice = data.schoolContract.contractPrice
                this.dataForm.contractType = data.schoolContract.contractType
                this.dataForm.contractDesc = data.schoolContract.contractDesc
                this.dataForm.paymentProgress = data.schoolContract.paymentProgress
                this.dataForm.storageLocation = data.schoolContract.storageLocation
                this.dataForm.signingTime = data.schoolContract.signingTime
                this.dataForm.inputPerson = data.schoolContract.inputPerson
                this.dataForm.inputTime = data.schoolContract.inputTime
              }
            })
          }
        })
      },

      myFormatDate(time){
        var date = new Date(time);

        var year = date.getFullYear(),
          month = date.getMonth() + 1,//月份是从0开始的
          day = date.getDate(),
          hour = date.getHours(),
          min = date.getMinutes(),
          sec = date.getSeconds();
        var newTime = year + '-' +
              month + '-' +
              day + ' ' +
              hour + ':' +
              min + ':' +
              sec;
        return newTime;			
      },
      // 表单提交
      dataFormSubmit () {
        // console.log(this.contractDateRange[0].getDate())
        // console.log(this.myFormatDate(this.contractDateRange[0]))

        this.dataForm.startTime = this.contractDateRange[0].getTime
        this.dataForm.endTime = this.contractDateRange[1].getTime

        console.log(this.dataForm)

        // return
        // this.$refs['dataForm'].validate((valid) => {
        //   if (valid) {
            // this.$http({
            //   url: this.$http.adornUrl(`/contract/schoolcontract/${!this.dataForm.id ? 'save' : 'update'}`),
            //   method: 'post',
            //   data: this.$http.adornData({
            this.$axios.post(`http://localhost:10001/contract/schoolcontract/${!this.dataForm.id ? 'save' : 'update'}`, {
                'id': this.dataForm.id || undefined,
                'contractName': this.dataForm.contractName,
                'contractNum': this.dataForm.contractNum,
                'firstParty': this.dataForm.firstParty,
                'secondParty': this.dataForm.secondParty,
                // 'startTime': this.dataForm.startTime,
                // 'endTime': this.dataForm.endTime,
                'startTime': this.contractDateRange[0],
                'endTime': this.contractDateRange[1],
                'contractPrice': this.dataForm.contractPrice,
                'contractType': this.dataForm.contractType,
                'contractDesc': this.dataForm.contractDesc,
                'paymentProgress': this.dataForm.paymentProgress,
                'storageLocation': this.dataForm.storageLocation,
                'signingTime': this.dataForm.signingTime,
                'inputPerson': this.dataForm.inputPerson,
                'inputTime': this.dataForm.inputTime
              // })
            }).then(({data}) => {
              console.log("保存的合同数据为：",data);

              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
        //   }
        // })
      }
    }
  }
</script>
